/*
 * Copyright (c) 2008, Hugh Sanderson, http://gamehaxe.com/
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 */
 
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Rectangle;

import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.LineScaleMode;
import flash.geom.Matrix;
import flash.events.Event;
import flash.text.TextField;


class ScrollRect extends Sprite
{
   static function main()
   {
      neash.Lib.Init("ScrollRect",640,480,false,true); 
      neash.Lib.SetBackgroundColour(0xffffff); 

      new ScrollRect();

      neash.Lib.SetFrameRate(1000);
      neash.Lib.ShowFPS();
      neash.Lib.Run(); 
   }

   var scene : Sprite;
   var mXPos:Int;
   var mDir:Int;

   public function new()
   {
      super();
      flash.Lib.current.addChild(this);

      scene = new Sprite();
         addChild(scene);
      var gfx = scene.graphics;

      gfx.beginFill(0x5050ff);
      gfx.drawRect(0,0,1000,300);
      gfx.beginFill(0x808050);
      gfx.drawRect(0,300,1000,640);


      var colours = [  0xffffff, 0x008000,  ];
      var alphas = [ 1.0, 1.0 ];
      var ratios = [ 80, 81 ];
      var mtx = new Matrix();
      mtx.createGradientBox(100,140,Math.PI/2,150,100);


      gfx.lineStyle(2,0x000000);
      gfx.beginGradientFill(GradientType.LINEAR,colours,alphas,ratios,mtx);
      gfx.moveTo(-20,320);
      gfx.lineTo(-20,200);
      for(i in 0...50)
      {
         gfx.lineTo(i*20, 100+Math.random()*200);
      }

      gfx.lineTo(1020, 320);
      gfx.lineTo(-20, 320);

      mXPos = 0;
      mDir = 1;

      scene.x = 20;
      scene.y = 50;
      //scene.rotation = 60;

      var text = new TextField();
      text.x = 200;
      text.y = 200;
      var fmt = new flash.text.TextFormat();
      fmt.size = 32;
      fmt.color = 0x005000;
      text.setTextFormat(fmt);
      text.text = "Hello World.  These mist covered mountains, are home now to me.";
      text.autoSize = flash.text.TextFieldAutoSize.LEFT;
      scene.addChild(text);

      for(i in 0...10)
      {
         var tree = new Shape();
         var gfx = tree.graphics;
         gfx.lineStyle(1,0);
         gfx.beginFill(0xa0a050);
         gfx.drawRect(-10,20,20,50);

         gfx.beginFill(0x00a000);
         gfx.drawCircle(0,0,30);
         tree.x = 20 + Math.random()*1000;
         tree.y = 300 + i*10;
         scene.addChild(tree);
      }


      addEventListener(Event.ENTER_FRAME, OnEnterFrame);
   }

   function OnEnterFrame(inEvent:Event)
   {
      mXPos+=mDir;
      if (mXPos>1000-640 || mXPos<0)
         mDir *= -1;

      var rect = new Rectangle(mXPos,50,600,380);
      scene.scrollRect = rect;
   }
}
